package com.reg;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017-05-18.
 */
public class SequenceRegular implements IRegular {

    ArrayList<IRegular> regulars = new ArrayList<>();

    public SequenceRegular(){}

    public SequenceRegular( List<IRegular> regulars){
        this.regulars.addAll(regulars);
    }


    public SequenceRegular addRegular(IRegular regular){
        regulars.add(regular);
        return this;
    }

    public List<String> match(char[] chars){
        if(regulars.size() == 0)return null;
        List<String> re = regulars.get(0).match(chars);
        if(re==null || regulars.size() == 1 || re.size() == 0)return re;
        ArrayList<String> strings = new ArrayList<String>();
        SequenceRegular left = new SequenceRegular(regulars.subList(1,regulars.size()));
        for(int i = 0 ;i < re.size();i++){
            List<String> leftMatchedStrings = left.match(Arrays.copyOfRange(chars, re.get(i).length(), chars.length));
            if(leftMatchedStrings!=null){
                for(int j = 0; j < leftMatchedStrings.size();j++){
                    strings.add(re.get(i) + leftMatchedStrings.get(j));
                }
            }
        }
        return strings;
    }

    int getSequenceCount(){
        return regulars.size();
    }
}
